var WxParse = require('../../wxParse/wxParse.js');
const app = getApp()
var service = require('../../util/service')
var util = require('../../util/util')
Page({
  data: {
    news: {},
    id: "",
    zan: {
      state: false,
      icon: "../../images/photo/zan.png"
    },
    comment: {
      state: false,
      icon: "../../images/photo/comment.png"
    },
    collection: {
      state: false,
      icon: "../../images/photo/collection.png"
    },
    type:""
  },
  onLoad: function (options) {
    var that = this;
    console.log(options);
    var user = app.userInfo;
    var id = options.id;
    var type = options.type;
    var title = options.title||"新闻";
    wx.setNavigationBarTitle({
      title: title
    }) 
    if(type == "SP"){
      this.setData({
        type: "SP"
      })
      service.AJAX("smc/base/SystemProduct/findSystemProduct.json", { id: options.id }, res => {
        WxParse.wxParse('article', 'html', res.prodDesc, that, 5);
      }, err => { }, 'POST')
    }else{
      service.AJAX("smc/topic/NewTopics/findNewTopics.json", { id: options.id }, res => {
        WxParse.wxParse('article', 'html', res.topicContent, that, 5);
        var data = util.getTime(res.createdAt);
        if (data.length > 10) res.createdAt = data.substr(0, 10);
        this.setData({
          news: res,
          id: id
        })
        var lookNumber = res.lookNumber || 0;
        var nnews = {
          topicId: res.topicId,
          lookNumber: lookNumber + 1
        }
        //统计评论数量
        service.AJAX(`smc/topic/BasicComments/groupBasicComments.json?$group-additions=count-1&$relId=${id}&$relType=Topic`, {}, res => {
          this.data.news.scoreNum = res;
          this.setData({
            news: this.data.news
          });
        }, err => { }, 'POST')
        //更新org的浏览量
        service.AJAX(`smc/topic/NewTopics/updNewTopics.do`, nnews, res => { }, err => { }, "POST");
      }, err => { }, 'POST')
      //获取点赞集合判断用户是否对资讯进行过点赞
      service.AJAX(`smc/dim/RecordPraiseRel/listRecordPraiseRel.json?$relType=Topic&$relId=${options.id}&$userId=${user.buyId}`, {}, res => {
        if (res.length > 0) {
          this.setData({
            zan: { state: true, icon: "../../images/photo/zans.png" }
          });
        }
      }, err => { }, 'GET', true)
      //通过用户收藏字段判断用户是否对该资讯进行过收藏
      //{provider:"12,34,56",topic:"11,22,33"}格式
      var buyc = user.buyCollection;
      if (buyc && buyc != "" && buyc != "null") {
        var buyobj = JSON.parse(buyc);
        if (buyobj) {
          var topic = buyobj.topic;
          var len = ("," + options.id).length;
          //标示以收藏
          if (topic && topic.length > 0 && (topic.indexOf(options.id + ",") == 0 || topic.indexOf("," + options.id + ",") >= 0 || topic.indexOf("," + options.id) == (topic.length - len))) {
            this.setData({
              collection: { state: true, icon: "../../images/photo/collections.png" }
            });
          }
        }
      }
    }
    
    
  },
  zans: function () {
    var zan = this.data.zan;
    zan.state = !zan.state;
    //获取点赞集合判断用户是否对改机构进行过点赞
    service.AJAX2(`smc/dim/RecordPraiseRel/dealRecordPraiseRel.do`, { relId: this.data.id, userId: app.userInfo.buyId, relType: 'Topic' }, res => {
      if (res > 0) {
        var title = "";
        var news = this.data.news;
        news.thumbsNumber = news.thumbsNumber||0;
        if (zan.state) {
          zan.icon = "../../images/photo/zans.png";
          title = "点赞成功";
          news.thumbsNumber = news.thumbsNumber + 1;
        } else {
          zan.icon = "../../images/photo/zan.png";
          title = "取消点赞成功";
          news.thumbsNumber = news.thumbsNumber - 1;
        }
        wx.showToast({
          title: title,
          icon: 'succes',
          duration: 1000,
          mask: true
        })
        this.setData({
          zan: zan,
          news: news
        });
        var nnews = {

        }
        //更新org的点赞数
        service.AJAX(`smc/topic/NewTopics/updNewTopics.do`, news, res => { }, err => { }, "POST");
      } else {
        wx.showToast({
          title: "系统异常请稍后再试",
          icon: 'error',
          duration: 1000,
          mask: true
        })
      }
    }, err => { }, 'GET', true)
  },
  collections: function () {
    var collection = this.data.collection;
    collection.state = !collection.state;
    var title = "";
    var user = app.userInfo;
    var buyc = (user.buyCollection && user.buyCollection != "null") ? user.buyCollection : "{}";
    var buyobj = JSON.parse(buyc);
    var topic = buyobj.topic || "";
    var buycs = topic.split(",");
    var news = this.data.news;
    var a3 = news.score||0;
    if (collection.state) {
      collection.icon = "../../images/photo/collections.png";
      title = "收藏成功";
      buycs.push(this.data.id);
      news.score = a3 + 1
    } else {
      collection.icon = "../../images/photo/collection.png";
      title = "取消收藏成功";
      var newbuycs = [];
      for (var i = 0; i < buycs.length; i++) {
        if (buycs[i] != this.data.id) newbuycs.push(buycs[i]);
      }
      buycs = newbuycs;
      news.score = a3 - 1
    }
    wx.showToast({
      title: title,
      icon: 'succes',
      duration: 1000,
      mask: true
    })
    buyobj.topic = buycs.toString();
    user.buyCollection = JSON.stringify(buyobj);
    var nuser = {
      buyId: user.buyId, buyCollection: user.buyCollection
    };
    //更新用户收藏项
    service.AJAX(`smc/base/SystemBuyerUser/updSystemBuyerUser.do`, nuser, res => { }, err => { }, "POST");
    console.log(news);
    var nnews = {
      topicId: news.topicId,
      score: news.score
    }

    //更新org的收藏数
    service.AJAX(`smc/topic/NewTopics/updNewTopics.do`, nnews, res => { }, err => { }, "POST");
    this.setData({
      collection: collection,
      news: news
    });
  },
  comments: function () {
    var comment = this.data.comment;
    comment.state = !comment.state;
    if (comment.state) {
      comment.icon = "../../images/photo/comments2.png";
    } else {
      comment.icon = "../../images/photo/comment.png";
    }
    this.setData({
      comment: comment
    });
    wx.navigateTo({
      url: '../comment/comment?type=Topic&id=' + this.data.news.topicId
    })
  }


})
